/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.format;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import org.llama.jmex.terra.HeightMapManager;
import org.llama.jmex.terra.MapCompressor;

public class ZLibMapCompressor implements MapCompressor {

	
	public ZLibMapCompressor() {
	}
	
	public ZLibMapCompressor(int compressionLevel, int compressionStrategy) {
		this.compressionLevel = compressionLevel;
		this.compressionStrategy = compressionStrategy;
	}	
	
	private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
    public void setCompressionLevel(int level) {
        compressionLevel = level;
    }

    private int compressionStrategy = Deflater.DEFAULT_STRATEGY;
    public void setCompressionStrategy(int strategy) {
        compressionStrategy = strategy;
    }
    
    private byte[] inputbuffer, outputbuffer;
    
	public synchronized ByteBuffer decompressMap(HeightMapManager man, int x, int y,
			ByteBuffer source) throws IOException {
		ByteBuffer dest = man.createMap(x, y);
		Inflater inf = new Inflater();
    	source.rewind();
		if (source.hasArray()) {
    		inf.setInput(source.array(), source.arrayOffset(), source.limit());
    	}
    	else {
    		if (inputbuffer == null || inputbuffer.length < source.limit())
    			inputbuffer = new byte[source.limit()];
    		source.get(inputbuffer, 0, source.limit());
    		inf.setInput(inputbuffer, 0, source.limit());
    	}
    	
    	dest.rewind();
    	
    	if (dest.hasArray()) {
    		try {
				inf.inflate(dest.array(), dest.arrayOffset(), dest.limit());
			} catch (DataFormatException e) {
				throw new IOException("Data format is not correct");
			}
    	}
    	else {
    		if (outputbuffer == null || outputbuffer.length < dest.limit())
    			outputbuffer = new byte[dest.limit()];
    		try {
				inf.inflate(outputbuffer, 0, dest.limit());
			} catch (DataFormatException e) {
				throw new IOException("Data format is not correct");
			}			
			dest.put(outputbuffer, 0, dest.limit());
			dest.rewind();
    	}
    	inf.end();
    	return dest;
	}

	public synchronized ByteBuffer compressMap(HeightMapManager man, int x, int y,
			ByteBuffer source) throws IOException {
		
		Deflater def = new Deflater(compressionLevel);
		def.setStrategy(compressionStrategy);

		source.rewind();
    	
    	if (source.hasArray()) {
    		def.setInput(source.array(), source.arrayOffset(), source.limit());
    	}
    	else {
    		if (inputbuffer == null || inputbuffer.length < source.limit())
    			inputbuffer = new byte[source.limit()];
    		source.get(inputbuffer, 0, source.limit());
    		def.setInput(inputbuffer, 0, source.limit());    		
    	}
    	def.finish();
    	// in theory a compressed map can be a bit bigger than the uncompressed map.
    	int maxbuffer = source.limit() + (source.limit() / 4);  
    	if (outputbuffer == null || outputbuffer.length < maxbuffer)
			outputbuffer = new byte[maxbuffer];
    	
    	int compressed = def.deflate(outputbuffer);
    	System.out.println("compressed! "+compressed+" "+def.getBytesWritten()+" "+def.getBytesRead());
    	def.end();
    	ByteBuffer dest = man.createCompressedMap(x, y, compressed);
    	dest.rewind();
    	dest.put(outputbuffer, 0, compressed);
    	return dest;
	}

}
